package com.hdax.dmw.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dmw.common.utils.Result;
import com.hdax.dmw.common.utils.ResultGenerator;
import com.hdax.dmw.common.utils.TokenUtil;
import com.hdax.dmw.entity.user.User;
import com.hdax.dmw.user.dtos.LoginDto;
import com.hdax.dmw.user.service.UserService;
import com.hdax.dmw.user.mappers.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
* @author guoyisha
* @description 针对表【dm_user(用户表)】的数据库操作Service实现
* @createDate 2023-06-18 15:13:28
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Override
    public Result<LoginDto> login(String phone) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User user = getOne(queryWrapper);
        if(user == null){
            //手机号未注册，或者填写错误
        }
        LoginDto dto = new LoginDto();
        Map<String,Object> params = new HashMap<>();
        params.put("id",user.getId()+"");
        params.put("phone",phone);
        params.put("nickName",user.getNickName());
        String token = TokenUtil.createToken(params);
        dto.setToken(token);
        Long genTime = System.currentTimeMillis();
        dto.setGenTime(genTime);
        dto.setExtTime(genTime + TokenUtil.TOKEN_EXPIRE_TIME);
        dto.setNickName(user.getNickName());
        return ResultGenerator.genSuccessResult(dto);
    }

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result<LoginDto> login(String phone, String password) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User user = getOne(queryWrapper);
        if(user == null){
            //手机号未注册，或者填写错误
        }else{
            if(!user.getPassword().equals(password)){
                //密码不正确
            }
        }
        LoginDto dto = new LoginDto();
        Map<String,Object> params = new HashMap<>();
        Map<String,Object> redisMap = new HashMap<>();
        params.put("id",user.getId()+"");
        params.put("phone",phone);
        params.put("nickName",user.getNickName());
        String token = TokenUtil.createToken(params);
        dto.setToken(token);
        Long genTime = System.currentTimeMillis();
        dto.setGenTime(genTime);
        dto.setExtTime(genTime + TokenUtil.TOKEN_EXPIRE_TIME);
        dto.setNickName(user.getNickName());

        //存储用户信息到redis
        redisMap.put("id",user.getId());
        redisMap.put("phone",phone);
        redisMap.put("nickName",user.getNickName());
        redisMap.put("token",token);
        redisTemplate.opsForHash().putAll("token:"+phone,redisMap);
        redisTemplate.expire("token:"+phone,TokenUtil.TOKEN_EXPIRE_TIME * 2L, TimeUnit.MILLISECONDS);
        return ResultGenerator.genSuccessResult(dto);
    }
}




